package com.chinawlx.wlxteacher.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chinawlx.wlxteacher.R;

import java.util.ArrayList;

import com.chinawlx.wlxteacher.widget.photoview.HackyViewPager;
import com.chinawlx.wlxteacher.widget.photoview.PhotoView;

/**
 * 作者:黄帅
 * 时间:16/9/2
 * 描述: 展示图片信息
 */
public class WLXPhotoViewActivity extends WLXBaseActivity {

    public static final String URLS = "picturesUrl";
    public static final String POSITION = "clickPosition";

    private ViewPager mViewPager;
    private ArrayList<String> mPicturesUrl;
    private int mClickPosition;
    private PhotoViewPagerAdapter mAdapter;
    private TextView mTvShowPosition;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photoview);
        init();
    }

    private void init() {
        initPassValue();
        initView();
    }

    private void initView() {
        if (mPicturesUrl == null || mPicturesUrl.size() == 0) {
            finish();
            return;
        }
        mTvShowPosition = (TextView) findViewById(R.id.tv_show_position);

        mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
        mAdapter = new PhotoViewPagerAdapter(mPicturesUrl);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(mClickPosition);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                position++;
                mTvShowPosition.setText(position + "/" + mPicturesUrl.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mTvShowPosition.setText((mClickPosition + 1) + "/" + mPicturesUrl.size());
    }

    private void initPassValue() {
        Intent intent = getIntent();
        if (intent == null) {
            finish();
            return;
        }
        mPicturesUrl = intent.getStringArrayListExtra(URLS);
        mClickPosition = intent.getIntExtra(POSITION, 0);
    }


    //适配器
    static class PhotoViewPagerAdapter extends PagerAdapter {
        private ArrayList<String> urls = new ArrayList<>();

        public PhotoViewPagerAdapter(ArrayList<String> tep) {
            urls.addAll(tep);
        }

        @Override
        public int getCount() {
            return urls.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());

            Glide.with(container.getContext()).load(urls.get(position)).placeholder(R.drawable
                    .place_holder).fitCenter().into(photoView);
            container.addView(photoView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
                    .LayoutParams.WRAP_CONTENT);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }
}
